package epoverty;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

/**
 *
 * @author Brent, Mike
 */
public class ImportProcessorData
{
    //Public array that will contain the data read from the the csv file
    public ProcessorData[] dataArray;
    //string with the file name and path
    private String file;

    //Constructor
    public ImportProcessorData()
    {
        //Init data array
        dataArray = new ProcessorData[0];
    }

    //Reads each line of data from the file in to a String collection object
    //then passes that object to be parsed in to ProcessorData objects
    public void parse(String filename)
    {
        //Collection to hold the lines from the file
        ArrayList<String> lines = new ArrayList<String>();
        try
        {
            //read the file in to the lines collection
            BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
            String line;
            while ((line = br.readLine()) != null)
            {
                lines.add(line);
            }

            //close the file
            br.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        //Parse the lines and fill the public dataArray with the read data
        fillDataArray(lines);
    }

    //Parses the lines read in to memory
    //splits the string based on the "," character
    //reads each part in to a temporary ProcessorData object
    //fills the public dataArray with the parsed data
    public void fillDataArray(ArrayList<String> lines)
    {
        //Collection of ProcessorData parsed from the lines in the file
        ArrayList<ProcessorData> pData = new ArrayList<ProcessorData>();

        //for every string in the lines collection
        for (String line : lines)
        {
            //skip header line
            if(line.contains("donation"))
                continue;

            //split the lines using the comma in to a string array
            String[] recordsOnLine = line.split(",");

            //if the string array has 3 elements we have a valid parse
            //read the data in to a ProcessorData object
            if (recordsOnLine.length == 3)
            {
                //Create data holding object
                ProcessorData pd = new ProcessorData();
                //read each part in to the object converting to the proper type
                pd.donationId = Integer.parseInt(recordsOnLine[0]);
                pd.donationAmount = new BigDecimal(recordsOnLine[1]);
                pd.statusID = Integer.parseInt(recordsOnLine[2]);

                //add the temp object to the main collection
                pData.add(pd);
            }
        }

        //Fill the public dataArray with the data we parsed from the lines collection
        dataArray = (ProcessorData[]) pData.toArray(new ProcessorData[0]);
    }

    public void parseFile(String filePath)
    {
        this.file = filePath;
        parse(filePath);
    }
}
